package org.lgiavedoni.android.franq.util.thread;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.lgiavedoni.android.franq.service.log.Logger;
import org.lgiavedoni.android.franq.service.phone.ContextService;
import org.lgiavedoni.android.franq.util.date.DateUtil;

import android.content.Context;
import android.os.PowerManager;

public class ThreadUtil {

	public static void sleep(long time) {
		try {
			Logger.inst.debug("Sleeping for [" + time + "].");
			PowerManager.WakeLock wl = null;
			PowerManager pm = null;

			if (ContextService.inst.isApplicationContextLoaded()) {
				pm = (PowerManager) ContextService.inst.getApplicationContext().getSystemService(Context.POWER_SERVICE);
				wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Frank");
				wl.acquire();
			}

			Thread.sleep(time);

			if (ContextService.inst.isApplicationContextLoaded()) {
				wl.release();
			}

		} catch (Exception e) {
			Logger.inst.error(e);
		}
	}

	public static String wakeUp(long time) {
		return DateUtil.getDateHHmm(new Date(new Date().getTime() + time));
	}

	public static void sleepMinutes(int minutes) {
		ThreadUtil.sleep(DateUtil.getMillisecondsOfMins(minutes));
	}

	public static void sleepMinutes(long minutes) {
		ThreadUtil.sleep(DateUtil.getMillisecondsOfMins(Long.valueOf(minutes).intValue()));
	}

	public static void sleep(long time, String text) {
		Logger.inst.debug("Sleeping for [" + time + "]. " + text);
		try {
			Thread.sleep(time);
		} catch (Exception e) {
		}
	}

	/** Display info about a thread group */
	private static List<Thread> printGroupInfo(List<Thread> result, ThreadGroup g) {
		if (g == null)
			return result;
		int numThreads = g.activeCount();
		int numGroups = g.activeGroupCount();
		Thread[] threads = new Thread[numThreads];
		ThreadGroup[] groups = new ThreadGroup[numGroups];

		g.enumerate(threads, false);
		g.enumerate(groups, false);

		// System.out.println(indent + "Thread Group: " + g.getName() +
		// "  Max Priority: " + g.getMaxPriority() + (g.isDaemon() ? " Daemon" :
		// ""));

		for (int i = 0; i < numThreads; i++) {
			if (threads[i] != null)
				result.add(threads[i]);
		}
		for (int i = 0; i < numGroups; i++) {
			printGroupInfo(result, groups[i]);
		}
		return result;
	}

	/** Find the root thread group and list it recursively */
	public static List<Thread> listAllThreads() {
		ThreadGroup currentThreadGroup;
		ThreadGroup rootThreadGroup;
		ThreadGroup parent;

		// Get the current thread group
		currentThreadGroup = Thread.currentThread().getThreadGroup();

		// Now go find the root thread group
		rootThreadGroup = currentThreadGroup;
		parent = rootThreadGroup.getParent();
		while (parent != null) {
			rootThreadGroup = parent;
			parent = parent.getParent();
		}

		return printGroupInfo(new ArrayList<Thread>(), rootThreadGroup);
	}

	public static void main(String[] args) {
		System.out.println(listAllThreads());
	}

}
